<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="common.CommonUtil"%>
<%@ page import="common.TameUtil"%>
<%@ page import="common.ThreadWorker"%>
<%@ page import="bean.*"%>
<%@ page import="java.util.*"%>
<%@ page import="servlets.BAIGenerator"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Save_file</title>
<link rel="stylesheet" type="text/css" href="./css/common.css" />
<script src="./js/jquery-1.7.js"></script>

<style type="text/css">
a,tr,td {
	text-decoration: none;
}

.entryLink {
	position : relative;
}

#content {
	position: relative;
	width: 600px;
	height: 380px;
	letter-spacing: 1.0pt;
	word-spacing: 0.6pt;
	line-height: 0.9;
	background-color: #111;
	border: 1px solid #666;
	color: white;
	opacity: 0.99;
	margin: 1em 1em 1.4em 1em;
	overflow: auto;
}

.logo {
	margin-top: 5px;
}

#header {
	width: 750px;
	height: 70px;
	margin-top: 30px;
	margin-left: auto;
	margin-right: auto;
	width: 750px;
}

#container {
	position: relative;
	width: 750px;
	height: 450px;
	margin-left: auto;
	margin-right: auto;
}

#prv {
	position: relative;
	left: 15px;
}

#savefile {
	position: relative;
	top: -10px;
	left: 10px;
}
</style>
</head>
<body>
	<%
		TameUtil.setStart();

		String urlStr = BAIGenerator.getFileNameRequestURL();

		TameUtil.setEnd();
		TameUtil.setTimeSpan();

		String fileName = new ThreadWorker(urlStr).fetchFileName();

		String rawHeaderContent = BAIGenerator.getHeadRawContent();	// header content of bai file
		String rawFootContent = BAIGenerator.getFootRawContent();	// footer content of bai file

		// common field values
		String dateVal = request.getParameter("date");
		String timeVal = request.getParameter("time");
		
		int sectionNum = Integer.parseInt(request.getParameter("sectionNum"));
		
		String amountPrefix = "amount_";
		String epnPrefix = "epn_";
		String bnPrefix = "bn_";
		String bbanPrefix = "bban_";
		String ibanPrefix = "iban_";
		
		double sumInDollar = 0.0;
		for(int i=0;i<sectionNum;i++) {
			sumInDollar += Double.parseDouble(request.getParameter(amountPrefix+i));
		}
		long sumInCent = (long)(sumInDollar*100);
		
		rawHeaderContent = rawHeaderContent.replaceAll("<date>", dateVal).replaceAll("<time>", timeVal).replaceAll("<sum>", String.valueOf(sumInCent));
		rawFootContent = rawFootContent.replaceAll("<dsum>",String.valueOf(sumInCent<<1));
		
		Set<ITemplate> templateSet = BAIGenerator.getTemplates();
		Iterator<ITemplate> iter = templateSet.iterator();
		ITemplate templateA = null;
		ITemplate templateB = null;
		ITemplate templateC = null;
		ITemplate temp = null;
		while(iter.hasNext()) {
			temp = iter.next();
			if(temp.getType().equals("TemplateA")) {
				templateA = temp;
			}
			else if(temp.getType().equals("TemplateB")) {
				templateB = temp;
			}
			else if(temp.getType().equals("TemplateC")) {
				templateC = temp;
			}
		}
		String rawContentA = templateA.getContent();
		String rawContentB = templateB.getContent();
		String rawContentC = templateC.getContent();
		
		String content = "";// the final content of the bai file
		content += rawHeaderContent;
		String[] bodyContents = new String[sectionNum];
		String amountX=null , epnX=null , bnX=null , bbanX=null , ibanX = null;
		for(int i=0;i<sectionNum;i++) {
			amountX = String.valueOf((long)(100*Double.parseDouble( request.getParameter(amountPrefix+i))));
			epnX = request.getParameter(epnPrefix+i);
			bnX = request.getParameter(bnPrefix+i);
			bbanX = request.getParameter(bbanPrefix+i);
			ibanX = request.getParameter(ibanPrefix+i);
			String tempRaw = null;
			if(bbanX != null) {//TemplateB Section
				
				tempRaw = rawContentB.replaceAll("<amount>", amountX).replaceAll("<epn>", epnX).replaceAll("<bn>", bnX).replaceAll("<van_bban>", bbanX);
				bodyContents[i] = tempRaw;
				
			}else if(ibanX != null){//TemplateC Section
				tempRaw = rawContentC.replaceAll("<amount>", amountX).replaceAll("<epn>", epnX).replaceAll("<bn>", bnX).replaceAll("<van_iban>", ibanX);
				bodyContents[i] = tempRaw;
				
			}else{//TemplateA Section
				tempRaw = rawContentA.replaceAll("<amount>", amountX).replaceAll("<epn>", epnX).replaceAll("<bn>", bnX);
				bodyContents[i] = tempRaw;
			}
			
			content += bodyContents[i];
		}
		content += rawFootContent;		
		
	%>
	<div id="header">
		<div class="logo">
			<img alt="logo" src="./img/logo.gif">
		</div>
	</div>
	<div id="container">
		<label id="prv">Preview</label> <br>
		<textarea id="content" disabled="disabled"><%=content%></textarea>
		<br>

		<form action="SaveFile" method="POST">
			<input type="hidden" id="fn" name="fn" value="<%=fileName%>" /> <input
				type="hidden" id="content" name="content" value="<%=content%>" />
			<input type="submit" id="savefile" value="Save" />
		</form>
	</div>
</body>
</html>